import Vue from 'vue'
import Cube from 'cube-ui'
import Vant from 'vant';
import 'vant/lib/index.css';
import App from './App'
import router from './router/index'
import store from './store/index'
import rq from './requests'
import global from "./global";
import VueSocketIO from 'vue-socket.io';
// import SocketIO from "socket.io-client";
if (!window.AudioContext) {
  if (!window.webkitAudioContext) {
    alert(
      "Your browser does not support any AudioContext and cannot play back this audio."
    );
  }
  window.AudioContext = window.webkitAudioContext;
}
const audioContext = new AudioContext();
Vue.prototype.ac = audioContext;
Vue.prototype.rq = rq;
Vue.prototype.global = global;
Vue.use(Vant);
Vue.directive("longpress", {
  bind: function(el, binding, vNode) {
    // Make sure expression provided is a function
    if (typeof binding.value !== "function" && vNode.context !== undefined) {
      // pass warning to console
      let warn = `[longpress:] provided expression '${binding.expression}' is not a function, but has to be`;

      console.warn(warn);
    }

    // Define variable
    let pressTimer = null;

    // Define funtion handlers
    // Create timeout ( run function after 1s )
    let start = (e) => {
      if (e instanceof MouseEvent && e.type === "click" && e.button !== 0) {
        return;
      }

      if (pressTimer === null) {
        pressTimer = setTimeout(() => {
          // Run function
          handler(e);
        }, 1000);
      }
    };

    // Cancel Timeout
    let cancel = () => {
      // Check if timer has a value or not
      if (pressTimer !== null) {
        clearTimeout(pressTimer);
        pressTimer = null;
      }
    };
    // Run Function
    const handler = (e) => {
      binding.value(e);
    };

    // Add Event listeners
    el.addEventListener("mousedown", start);
    el.addEventListener("touchstart", start);
    // Cancel timeouts if this events happen
    el.addEventListener("click", cancel);
    el.addEventListener("mouseup", cancel);
    el.addEventListener("mouseout", cancel);
    el.addEventListener("touchend", cancel);
    el.addEventListener("touchcancel", cancel);
  }
});


Vue.config.productionTip = false

Vue.use(new VueSocketIO({
  debug: true,
  connection: 'http://127.0.0.1:9089',
}));


/* eslint-disable no-new */
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
